package net.tycmc.cty.ctyms.web.Dao;

import java.util.List;
import java.util.Map;

import com.tycmc.functions.util.MapGetter;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.bulb.dbutil.pager.Pager;
import net.tycmc.bulb.dbutil.pager.PagerModel;
import net.tycmc.cty.ctyms.timejob.common.MyBaseDAO;

public class DBManagePlatFormDao {
	private IBaseDAO baseDAO;
	private MyBaseDAO mbd;
	public MyBaseDAO getMbd() {
		return mbd;
	}

	public void setMbd(MyBaseDAO mbd) {
		this.mbd = mbd;
	}

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	public PagerModel getDBList(Pager page,String flag){
		String sql= "select DBInfo_ID, "+
				    "DBInfo_IP, "+//IP
					"DBInfo_Port, "+//端口
					"DBInfo_DBServerName, "+//数据库服务器名称
					"DBInfo_UserName, "+//用户名
					"DBInfo_Password, "+//密码
					"DBInfo_Type," +//数据库类型      数据库类型（1：CTY_Vcl；2：CTY_Egn_A_主库；3：CTY_Egn_B_主库；4：解码月库；5：文件服务器）
					/*"=case when DBInfo_Type='1' then cast(DBInfo_Type as varchar)+':CTY_Vcl' " +
					"when DBInfo_Type='2' then cast(DBInfo_Type as varchar)+':CTY_Egn_A' " +
					"when DBInfo_Type='3' then cast(DBInfo_Type as varchar)+':CTY_Egn_B' " +
					"when DBInfo_Type='4' then cast(DBInfo_Type as varchar)+':解码月库' " +
					"else cast(DBInfo_Type as varchar)+':' end, "+*/
					"DBInfo_DBSLongName,"+
					"convert(varchar(20),DBInfo_DateMonth,120)DBInfo_DateMonth, "+//月库月份
					"DBInfo_DBName, "+//数据库名称
					"DBInfo_Remark, "+//备注
					"DBInfo_OprtName, "+//操作人
					"convert(varchar(20),DBInfo_Eff,120)DBInfo_Eff, "+//生效日期
					"convert(varchar(20),DBInfo_Exp,120)DBInfo_Exp " +//失效日期
					"from DataBaseInfo " +
					"where DBInfo_Eff<getdate() and DBInfo_Exp>getdate()";
		return mbd.getPager2015(sql,new Object[]{}, page,flag.equals("true")?true:false);
	}
	//修改
	public int updateDbInfo(Map<String,Object>map){
		String id=MapGetter.getString(map,"id");
		String ip=MapGetter.getString(map, "ip");
		String port=MapGetter.getString(map, "port");
		String dbservername=MapGetter.getString(map, "dbservername");
		String username=MapGetter.getString(map, "username");
		String password=MapGetter.getString(map, "password");
		String type=MapGetter.getString(map,"type");
		String datemonth=MapGetter.getString(map,"datemonth");
		datemonth="null".equals(datemonth)||!StringUtil.isValid(datemonth)?null:datemonth;
		String dbname=MapGetter.getString(map,"dbname");
		String remark=MapGetter.getString(map,"remark");
		String oprtname=MapGetter.getString(map,"oprtname");
		String eff=MapGetter.getString(map,"eff");
		String exp=MapGetter.getString(map,"exp");
		String dbslongname=MapGetter.getString(map,"dbslongname");
		String sql="update DataBaseInfo set DBInfo_IP='"+ip+"',DBInfo_Port="+port+",DBInfo_DBServerName='"+dbservername+"',DBInfo_DBSLongName='"+dbslongname+"',  DBInfo_UserName='"+username+"',DBInfo_Password='"+password+"'," +
				"DBInfo_Type="+type+",DBInfo_DateMonth=?,DBInfo_DBName='"+dbname+"',DBInfo_Remark='"+remark+"',DBInfo_OprtName='"+oprtname+"',DBInfo_Eff='"+eff+"',DBInfo_Exp='"+exp+"' where DBInfo_ID="+id;
		return baseDAO.execUpdate(sql, new Object[]{datemonth});
	}
	//删除
	public int deleteDbInfo(String id,String OprtName,String remark){
		String sql="update DataBaseInfo set DBInfo_Exp=getdate(),DBInfo_OprtName='"+OprtName+"',DBInfo_Remark='"+remark+"' where  DBInfo_ID="+id;
		return baseDAO.execUpdate(sql, new Object[]{});
	}
	//新增
	public int AddDbInfo(Map<String,Object>map){
		String id=MapGetter.getString(map,"id");
		String ip=MapGetter.getString(map, "ip");
		String port=MapGetter.getString(map, "port");
		String dbservername=MapGetter.getString(map, "dbservername");
		String username=MapGetter.getString(map, "username");
		String password=MapGetter.getString(map, "password");
		String type=MapGetter.getString(map,"type");
		String datemonth=MapGetter.getString(map,"datemonth");
		if(!StringUtil.isValid(datemonth)){
			datemonth=null;
		}
		String dbname=MapGetter.getString(map,"dbname");
		String remark=MapGetter.getString(map,"remark");
		String oprtname=MapGetter.getString(map,"oprtname");
		String eff=MapGetter.getString(map,"eff");
		String exp=MapGetter.getString(map,"exp");
		String dbslongname=MapGetter.getString(map,"dbslongname");
		String sql="insert into  DataBaseInfo(DBInfo_IP,DBInfo_Port,DBInfo_DBServerName,DBInfo_DBSLongName,DBInfo_UserName,DBInfo_Password,DBInfo_Type,DBInfo_DateMonth,DBInfo_DBName,DBInfo_Remark,DBInfo_OprtName,DBInfo_Eff,DBInfo_Exp)" +
				" values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return baseDAO.execInsert(sql, new Object[]{ip,port,dbservername,dbslongname,username,password,type,datemonth,dbname,remark,oprtname,eff,exp});
	}
}
